package com.phkj.qdh.ui.fragment.drafts

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.commit
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.phkj.qdh.R
import com.phkj.qdh.bean.DraftBean
import com.phkj.qdh.databinding.FragmentMyDraftsBinding
import com.phkj.qdh.ui.adapter.DraftsAdapter
import com.phkj.qdh.utils.LogUtil
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.SPSUtils
import com.phkj.qdh.utils.SPS_DRAFTS
import rxhttp.wrapper.utils.GsonUtil

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 草稿箱
 */
class MyDraftsFragment : Fragment() {

    private var param1: String? = null
    private var param2: String? = null
    private var _binding: FragmentMyDraftsBinding? = null
    private val binding get() = _binding!!
    private val draftsAdapter by lazy { DraftsAdapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                parentFragmentManager.popBackStack()
            }

        })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentMyDraftsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack()
            }
            rv.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = draftsAdapter
                addItemDecoration(MyItemDecoration(width = 5))
            }
        }

        try {
            val json = SPSUtils.getString(SPS_DRAFTS, "")
            LogUtil.E(json)
            val data = GsonUtil.fromJson<ArrayList<DraftBean>>(
                json, object : TypeToken<ArrayList<DraftBean>>() {}.type
            )
            draftsAdapter.submitList(data)
        } catch (e: Exception) {

        }

        draftsAdapter.addOnItemChildClickListener(R.id.tv_edit) { adapter, _, pos ->
            val data = adapter.getItem(pos)
            when (data!!.type) {
                1 -> {
                    parentFragmentManager.commit {
                        addToBackStack("draft")
                        replace(
                            id, DraftBaseFragment.newInstance(data.base!!,data.id )
                        )
                        setReorderingAllowed(true)
                    }
                }
                2 -> {
                    parentFragmentManager.commit {
                        addToBackStack("draft")
                        replace(
                            id, DraftTeamFragment.newInstance(data.team!!, data.id)
                        )
                        setReorderingAllowed(true)
                    }
                }
                3 -> {
                    parentFragmentManager.commit {
                        addToBackStack("draft")
                        replace(id, DraftLawFragment.newInstance(data.law!!, data.id))
                        setReorderingAllowed(true)
                    }

                }
            }
        }
        draftsAdapter.addOnItemChildClickListener(R.id.tvdelet){ adapter, _, pos ->
            adapter.removeAt(pos)
            SPSUtils.put(SPS_DRAFTS, GsonUtil.toJson(adapter.items))
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) = MyDraftsFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}